/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mkt.view;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import mkt.util.Conexao;

/**
 *
 * @author Claudio Leite
 */
public class TrabalhoMKTMDI extends javax.swing.JFrame {

    private static Connection conexao;
    public FormAvaliacao formAvaliacao;
    public FormRecomenda formRecomenda;
    public FormLivro formLivro;

    /**
     * Creates new form TrabalhoMKTMDI
     */
    public TrabalhoMKTMDI() {
        
        initComponents();
        this.setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        desktopPane = new javax.swing.JDesktopPane();
        menuBar = new javax.swing.JMenuBar();
        mnuArquivo = new javax.swing.JMenu();
        openMenuNovaAvaliacao = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        mnuEditar = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        mnuAjuda = new javax.swing.JMenu();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        desktopPane.setName(""); // NOI18N

        mnuArquivo.setMnemonic('f');
        mnuArquivo.setText("Avaliação");

        openMenuNovaAvaliacao.setMnemonic('o');
        openMenuNovaAvaliacao.setText("Nova Avaliação");
        openMenuNovaAvaliacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openMenuNovaAvaliacaoActionPerformed(evt);
            }
        });
        mnuArquivo.add(openMenuNovaAvaliacao);

        exitMenuItem.setMnemonic('x');
        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        mnuArquivo.add(exitMenuItem);

        menuBar.add(mnuArquivo);

        mnuEditar.setMnemonic('e');
        mnuEditar.setText("Produtos");

        jMenuItem3.setText("Produtos Cadastrados");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        mnuEditar.add(jMenuItem3);

        menuBar.add(mnuEditar);

        jMenu1.setText("Recomendação");

        jMenuItem1.setText("Livros Recomendados");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        menuBar.add(jMenu1);

        mnuAjuda.setMnemonic('h');
        mnuAjuda.setText("Ajuda");

        aboutMenuItem.setMnemonic('a');
        aboutMenuItem.setText("Sobre");
        mnuAjuda.add(aboutMenuItem);

        menuBar.add(mnuAjuda);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 792, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 498, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitMenuItemActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        try {
            TrabalhoMKTMDI.conexao = Conexao.get();
            FormLogin dialog = new FormLogin(this, true);
            dialog.setVisible(true);
        } catch (SQLException ex) {
            JOptionPane.showInternalMessageDialog(this.desktopPane, String.format("Erro ao conectar ao SGBD: %s", ex.getMessage()), "Erro", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(TrabalhoMKTMDI.class.getName()).log(Level.SEVERE, null, ex);
            System.exit(1);
        } catch (ClassNotFoundException ex) {
            JOptionPane.showInternalMessageDialog(this.desktopPane, "Erro ao carregar o Drive do SGBD", "Erro", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(TrabalhoMKTMDI.class.getName()).log(Level.SEVERE, null, ex);
            System.exit(2);
        }

    }//GEN-LAST:event_formWindowOpened

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        try {
            conexao.close();
        } catch (SQLException ex) {
            Logger.getLogger(TrabalhoMKTMDI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NullPointerException ex) {
        }
    }//GEN-LAST:event_formWindowClosing

    private void openMenuNovaAvaliacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuNovaAvaliacaoActionPerformed
        // TODO add your handling code here:
    if (this.formAvaliacao == null) {
        this.formAvaliacao = new FormAvaliacao(this);
        this.desktopPane.add(this.formAvaliacao);
    }
    this.openJanela(this.formAvaliacao);
    }//GEN-LAST:event_openMenuNovaAvaliacaoActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
    if (this.formRecomenda == null) {
        this.formRecomenda = new FormRecomenda(this);
        this.desktopPane.add(this.formRecomenda);
    }
    this.openJanela(this.formRecomenda);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:
            // TODO add your handling code here:
    if (this.formLivro == null) {
        this.formLivro = new FormLivro(this);
        this.desktopPane.add(this.formLivro);
    }
    this.openJanela(this.formLivro);
    }//GEN-LAST:event_jMenuItem3ActionPerformed
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TrabalhoMKTMDI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TrabalhoMKTMDI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TrabalhoMKTMDI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TrabalhoMKTMDI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TrabalhoMKTMDI().setVisible(true);
            }
        });
    }
    private void openJanela(JComponent obj) {
        if (!obj.isVisible()) {
            if (!obj.isValid()) {
                desktopPane.add(obj);
            }

            obj.setVisible(true);
        }
    }

    /**
     * Retorna a conexão com a base de dados, realizada na abertura do MDI
     *
     * @return Conexão JDBC pronta
     */
    public static Connection getConexao() {
        return conexao;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JDesktopPane desktopPane;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenu mnuAjuda;
    private javax.swing.JMenu mnuArquivo;
    private javax.swing.JMenu mnuEditar;
    private javax.swing.JMenuItem openMenuNovaAvaliacao;
    // End of variables declaration//GEN-END:variables
}
